import { fileURLToPath, URL } from "node:url";
import path from "path";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import { UserConfigExport, ConfigEnv, loadEnv } from "vite";
import { viteMockServe } from "vite-plugin-mock";
//@ts-ignore
export default ({ command, mode }) => {
    // 配置服务器各种（生产，开发，测试）环境变量
    let env = loadEnv(mode, process.cwd());

    return {
        plugins: [
            vue(),
            createSvgIconsPlugin({
                // Specify the icon folder to be cached
                iconDirs: [path.resolve(process.cwd(), "src/assets/icons")],
                // Specify symbolId format
                symbolId: "icon-[dir]-[name]",
            }),
            viteMockServe({
                //@ts-ignore
                localEnabled: command === "serve",
                mockPath: "./src/mock",
            }),
        ],
        resolve: {
            alias: {
                "@": fileURLToPath(new URL("./src", import.meta.url)),
            },
        },
        css: {
            preprocessorOptions: {
                scss: {
                    javascriptEnabled: true,
                    additionalData: '@import "./src/style/variable.scss";',
                },
            },
        },
        // 代理跨越
        server: {
            proxy: {
                [env.VITE_APP_BASE_API]: {
                    // 网络路径
                    target: env.VITE_SERVE,
                    // 开启跨域
                    changeOrigin: true,
                    // 路径重写
                    rewrite: (path: any) => path.replace(/^\/api/, ""),
                },
            },
        },
        // build: {
        //     rollupOptions: {
        //         external: [
        //             "echarts",
        //             "echarts/lib/echarts",
        //             "echarts/lib/util/number",
        //         ],
        //     },
        // },
    };

    return {
        plugins: [vue()],
    };
};

// https://vitejs.dev/config/
// export default defineConfig();
